﻿namespace QLPhongKham.BaseClass.BenhNhan
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoBNBenhAnKB
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private int _deidbenhchinh;
        private int _deidbenhkemtheo;
        private int _deiddangnhap;
        private long _deidql;
        private int _iidbacsi;
        private int _iiddoituong;
        private int _iidphong;
        private string _sbenhchinh;
        private string _sbenhkemtheo;
        private string _sghichu;
        private string _smabn;
        private string _sngayvao;
        private string _sSchema;
        private string _strieuchung;
        private string _sxutri;

        public dtoBNBenhAnKB()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoBNBenhAnKB(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoBNBenhAnKB.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1   and ", columnIDQL, " = ", this._deidql }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoBNBenhAnKB.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoBNBenhAnKB.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoBNBenhAnKB.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoBNBenhAnKB.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnIDQL, ",", columnMABN, ",", columnIDPHONG, ",", columnNGAYVAO, ",", columnTRIEUCHUNG, ",", columnBENHCHINH, 
                    ",", columnBENHKEMTHEO, ",", columnIDDOITUONG, ",", columnXUTRI, ",", columnGHICHU, ",", columnIDBACSI, ",", columnIDBENHCHINH, ",", columnIDBENHKEMTHEO, ",", columnIDDANGNHAP, 
                    ") values(", this._deidql, ",'", this._smabn, "',", this._iidphong, ",to_timestamp('", this._sngayvao, "','dd/mm/yyyy hh24:mi'),:", columnTRIEUCHUNG, ",:", columnBENHCHINH, ",:", columnBENHKEMTHEO, ",", this._iiddoituong, 
                    ",'", this._sxutri, "',:", columnGHICHU, ",", this._iidbacsi, ",", this._deidbenhchinh, ",", this._deidbenhkemtheo, ",", this._deiddangnhap, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTRIEUCHUNG, NpgsqlDbType.Text, 200).Value = this._strieuchung;
                command.Parameters.Add(columnBENHCHINH, NpgsqlDbType.Text, 200).Value = this._sbenhchinh;
                command.Parameters.Add(columnBENHKEMTHEO, NpgsqlDbType.Text, 200).Value = this._sbenhkemtheo;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoBNBenhAnKB.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deidql = 0L;
            this._smabn = "";
            this._iidphong = 0;
            this._sngayvao = "";
            this._strieuchung = "";
            this._sbenhchinh = "";
            this._sbenhkemtheo = "";
            this._iiddoituong = 0;
            this._sxutri = "";
            this._sghichu = "";
            this._iidbacsi = 0;
            this._deidbenhchinh = 0;
            this._deidbenhkemtheo = 0;
            this._deiddangnhap = 0;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoBNBenhAnKB.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnIDQL, ",", columnMABN, ",", columnIDPHONG, ",to_char(", columnNGAYVAO, ",'dd/mm/yyyy hh24:mi') as ", columnNGAYVAO, ",", columnTRIEUCHUNG, ",", columnBENHCHINH, ",", columnBENHKEMTHEO, 
                    ",", columnIDDOITUONG, ",", columnXUTRI, ",", columnGHICHU, ",", columnIDBACSI, ",", columnIDBENHCHINH, ",", columnIDBENHKEMTHEO, ",", columnIDQL, ",", columnIDDANGNHAP, 
                    " from ", this._sSchema, ".", tableName, " where ", columnIDQL, "=", this._deidql
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deidql = long.Parse(dataTable.Rows[0][columnIDQL].ToString());
                    this._smabn = dataTable.Rows[0][columnMABN].ToString();
                    this._iidphong = int.Parse(dataTable.Rows[0][columnIDPHONG].ToString());
                    this._sngayvao = dataTable.Rows[0][columnNGAYVAO].ToString();
                    this._strieuchung = dataTable.Rows[0][columnTRIEUCHUNG].ToString();
                    this._sbenhchinh = dataTable.Rows[0][columnBENHCHINH].ToString();
                    this._sbenhkemtheo = dataTable.Rows[0][columnBENHKEMTHEO].ToString();
                    this._iiddoituong = int.Parse(dataTable.Rows[0][columnIDDOITUONG].ToString());
                    this._sxutri = dataTable.Rows[0][columnXUTRI].ToString();
                    this._sghichu = dataTable.Rows[0][columnGHICHU].ToString();
                    this._iidbacsi = int.Parse(dataTable.Rows[0][columnIDBACSI].ToString());
                    this._deidbenhchinh = int.Parse(dataTable.Rows[0][columnIDBENHCHINH].ToString());
                    this._deidbenhkemtheo = int.Parse(dataTable.Rows[0][columnIDBENHKEMTHEO].ToString());
                    this._deiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoBNBenhAnKB.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect, "", 0L);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoBNBenhAnKB.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(long idql)
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect, "", idql);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoBNBenhAnKB.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(string mabn)
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect, mabn, 0L);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoBNBenhAnKB.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect, string mabn, long idql)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnIDQL + "," + columnMABN + "," + columnIDPHONG + ",to_char(" + columnNGAYVAO + ",'dd/mm/yyyy hh24:mi') as ngay," + columnTRIEUCHUNG + "," + columnBENHCHINH + "," + columnBENHKEMTHEO + "," + columnIDDOITUONG + "," + columnXUTRI + "," + columnGHICHU + "," + columnIDBACSI + "," + columnIDBENHCHINH + "," + columnIDBENHKEMTHEO + "," + columnIDQL + "," + columnIDDANGNHAP + " from " + this._sSchema + "." + tableName + " where 1=1" + ((mabn == "") ? "" : (" and " + columnMABN + "='" + mabn + "'")), connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoBNBenhAnKB.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoBNBenhAnKB.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnMABN, " = '", this._smabn, "',", columnIDPHONG, " = ", this._iidphong, ",", columnNGAYVAO, " = to_timestamp('", this._sngayvao, 
                    "','dd/mm/yyyy hh24:mi'),", columnTRIEUCHUNG, " = :", columnTRIEUCHUNG, ",", columnBENHCHINH, " = :", columnBENHCHINH, ",", columnBENHKEMTHEO, " = :", columnBENHKEMTHEO, ",", columnIDDOITUONG, " = ", this._iiddoituong, 
                    ",", columnXUTRI, " = '", this._sxutri, "',", columnGHICHU, " = :", columnGHICHU, ",", columnIDBACSI, " = ", this._iidbacsi, ",", columnIDBENHCHINH, " = ", this._deidbenhchinh, 
                    ",", columnIDBENHKEMTHEO, " = ", this._deidbenhkemtheo, ",", columnIDDANGNHAP, " = ", this._deiddangnhap, " where ", columnIDQL, " = ", this._deidql
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTRIEUCHUNG, NpgsqlDbType.Text, 200).Value = this._strieuchung;
                command.Parameters.Add(columnBENHCHINH, NpgsqlDbType.Text, 200).Value = this._sbenhchinh;
                command.Parameters.Add(columnBENHKEMTHEO, NpgsqlDbType.Text, 200).Value = this._sbenhkemtheo;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoBNBenhAnKB.cs");
                return false;
            }
        }

        public static string columnBENHCHINH
        {
            get
            {
                return "benhchinh";
            }
        }

        public static string columnBENHKEMTHEO
        {
            get
            {
                return "benhkemtheo";
            }
        }

        public static string columnGHICHU
        {
            get
            {
                return "ghichu";
            }
        }

        public static string columnIDBACSI
        {
            get
            {
                return "idbacsi";
            }
        }

        public static string columnIDBENHCHINH
        {
            get
            {
                return "idbenhchinh";
            }
        }

        public static string columnIDBENHKEMTHEO
        {
            get
            {
                return "idbenhkemtheo";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDDOITUONG
        {
            get
            {
                return "iddoituong";
            }
        }

        public static string columnIDPHONG
        {
            get
            {
                return "idphong";
            }
        }

        public static string columnIDQL
        {
            get
            {
                return "idql";
            }
        }

        public static string columnMABN
        {
            get
            {
                return "mabn";
            }
        }

        public static string columnNGAYVAO
        {
            get
            {
                return "ngayvao";
            }
        }

        public static string columnTRIEUCHUNG
        {
            get
            {
                return "trieuchung";
            }
        }

        public static string columnXUTRI
        {
            get
            {
                return "xutri";
            }
        }

        public string pBENHCHINH
        {
            get
            {
                return this._sbenhchinh;
            }
            set
            {
                this._sbenhchinh = value;
            }
        }

        public string pBENHKEMTHEO
        {
            get
            {
                return this._sbenhkemtheo;
            }
            set
            {
                this._sbenhkemtheo = value;
            }
        }

        public string pGHICHU
        {
            get
            {
                return this._sghichu;
            }
            set
            {
                this._sghichu = value;
            }
        }

        public int pIDBACSI
        {
            get
            {
                return this._iidbacsi;
            }
            set
            {
                this._iidbacsi = value;
            }
        }

        public int pIDBENHCHINH
        {
            get
            {
                return this._deidbenhchinh;
            }
            set
            {
                this._deidbenhchinh = value;
            }
        }

        public int pIDBENHKEMTHEO
        {
            get
            {
                return this._deidbenhkemtheo;
            }
            set
            {
                this._deidbenhkemtheo = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._deiddangnhap;
            }
            set
            {
                this._deiddangnhap = value;
            }
        }

        public int pIDDOITUONG
        {
            get
            {
                return this._iiddoituong;
            }
            set
            {
                this._iiddoituong = value;
            }
        }

        public int pIDPHONG
        {
            get
            {
                return this._iidphong;
            }
            set
            {
                this._iidphong = value;
            }
        }

        public long pIDQL
        {
            get
            {
                return this._deidql;
            }
            set
            {
                this._deidql = value;
            }
        }

        public string pMABN
        {
            get
            {
                return this._smabn;
            }
            set
            {
                this._smabn = value;
            }
        }

        public string pNGAY
        {
            get
            {
                return this._sngayvao;
            }
            set
            {
                this._sngayvao = value;
            }
        }

        public string pTRIEUCHUNG
        {
            get
            {
                return this._strieuchung;
            }
            set
            {
                this._strieuchung = value;
            }
        }

        public string pXUTRI
        {
            get
            {
                return this._sxutri;
            }
            set
            {
                this._sxutri = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "bn_benhankb";
            }
        }
    }
}

